import {Component} from '@angular/core';
import {AlertController, IonicPage, LoadingController, NavController, NavParams, ToastController} from 'ionic-angular';
import {Api} from "../../providers/api/api"
import {TranslateService} from '@ngx-translate/core';
import {Browser} from '../../providers/providers';

/**
 * Generated class for the NewsPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-news',
  templateUrl: 'news.html',
})
export class NewsPage {

  items: Array<any> = [];
  kinds: Array<any> = [];
  kindStatus: Array<any> = [{value: 1, text: '连中'}, {value: 0, text: '连挂'}];
  private currentKind: any = {};
  private currentStatus: any = {};
  private params: any = {'page': 0, 'size': 20};
  private networkErrorString: string;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public translateService: TranslateService,
              public toastCtrl: ToastController,
              public loadingCtrl: LoadingController,
              public browser: Browser,
              public alerCtrl: AlertController,
              public api: Api) {
    this.translateService.get('NETWORK_ERROR').subscribe((value) => {
      this.networkErrorString = value;
    });
    this.initData();
    this.loadingCtrl.create({
      content: "数据加载中",
      duration: 500
    }).present();
  }

  getItems() {
    let reqParam = "?page=" + this.params.page + "&size=" + this.params.size + "&status=" + this.currentStatus.value + "&kindId=" + this.currentKind.id;
    let seq = this.api.get('cycle-plans/withWin' + reqParam).share();
    seq.subscribe((res: any) => {
      // If the API returned a successful response, mark the user as logged in
      if (res) {
        this.items = this.items.concat(res);
      } else {
        this.showToast().present();
      }
    }, err => {
      this.showToast().present();
      console.error('ERROR', err);
    });
    return seq;
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad NewsPage');
  }

  showToast() {
    return this.toastCtrl.create({
      message: this.networkErrorString,
      duration: 1000,
      position: 'top'
    });
  }


  doRefresh(refresher) {
    console.log('Begin async operation', refresher);
    this.params.page = 0;
    this.items = [];
    this.getItems();
    setTimeout(() => {
      refresher.complete();
    }, 500);
  }


  initData() {
    let seq = this.api.get('kinds').share();
    seq.subscribe((res: any) => {
      // If the API returned a successful response, mark the user as logged in
      if (res) {
        this.kinds = res;
        this.currentKind = this.kinds[0];
        this.currentStatus = this.kindStatus[0];

        this.getItems();

      } else {
        this.showToast().present();
      }
    }, err => {
      this.showToast().present();
      console.error('ERROR', err);
    });
    return seq;
  }

  doRadio() {
    let alert = this.alerCtrl.create();
    alert.setSubTitle('选择彩种');
    this.kinds.map((kind) => {
      alert.addInput({
        type: 'radio',
        label: kind.kind,
        value: kind.id
      });
    });
    alert.addButton({
      text: '确认',
      handler: data => {
        this.params.page = 0;
        this.kinds.forEach(value => {
          if (value.id === data) {
            this.currentKind = value;
          }
        });
        this.items = [];
        this.getItems();
      }
    });
    alert.present();
  }

  chooseStatus() {
    let alert = this.alerCtrl.create();
    alert.setSubTitle('选择状态');
    this.kindStatus.map((kind) => {
      alert.addInput({
        type: 'radio',
        label: kind.text,
        value: kind.value
      });
    });
    alert.addButton({
      text: '确认',
      handler: data => {
        this.params.page = 0;
        this.kindStatus.forEach(type => {
          if (type.value === data) {
            this.currentStatus = type;
          }
        });
        this.items = [];
        this.getItems();
      }
    });
    alert.present();
  }

  openDetail(planDetail) {
    this.navCtrl.push('PlanDetailPage', planDetail);
  }

  openPages(url) {
    this.browser.launch(url);
  }


  doInfinite(infiniteScroll) {
    this.params.page = this.params.page + 1;
    this.getItems();
    setTimeout(() => {
      infiniteScroll.complete();
    }, 1500);
  }

}
